package cn.wolfcode.crm.service.imp;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.query.EmployeeQueryObject;
import cn.wolfcode.crm.query.PageResult;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;


    public Employee selectByPrimaryKey(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    public List<Employee> selectAll() {
        return employeeMapper.selectAll();
    }

    public void importData(MultipartFile file) throws IOException {
        //创建excel文件,将文件导入Excel中
        Workbook wb = new HSSFWorkbook(file.getInputStream());
        //获取工作簿
        Sheet sheet = wb.getSheet("employeeData");
        //获取总行数
        int lastRowNum = sheet.getLastRowNum();
        //迭代每一行
        for (int i = 1; i <= lastRowNum ; i++) {
            //把每行的单元格的数据封装到对象中
            Row row = sheet.getRow(i);
            Employee emp = new Employee();
            emp.setUsername(row.getCell(0).getStringCellValue());
            emp.setRealname(row.getCell(1).getStringCellValue());
            emp.setTel(row.getCell(2).getStringCellValue());
            emp.setPassword(row.getCell(3).getStringCellValue());
            employeeMapper.insert(emp);
        }

    }

    public List<Employee> selectExportData(EmployeeQueryObject qo) {
        return employeeMapper.selectExportData(qo);
    }


    //页面信息对象,根据easyUI的DataGrid组件,返回的数据为json对象,且只有total,rows两个属性
    public PageResult query(EmployeeQueryObject qo) {
        int count = employeeMapper.selectCount(qo);
        if (count==0) {
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<Employee> rows = employeeMapper.queryForList(qo);
        return new PageResult(count,rows);
    }

    public Boolean selectByName(String username,Long id) {
        Employee emp = employeeMapper.selectByUsername(username);
        /*根据用户名查询用户是否存在,不存在就返回true,验证通过,
        * 如果用户存在,并且,它的id==前台的id,说明是编辑操作,也返回true,验证通过*/
        return emp==null||emp.getId()==id;
    }

    @Override
    public void changeState(Long id) {
        employeeMapper.updateStateById(id);
    }

    public void saveOrUPdate(Employee employee) {
        if (employee.getId() == null) {

            String password = employee.getPassword();
            /*密码加密*/
            Md5Hash md5 = new Md5Hash(password,employee.getUsername(),2);
            employee.setPassword(md5.toString());
            employeeMapper.insert(employee);
        }else{
            employeeMapper.deleteRoleRelation(employee.getId());
            employeeMapper.updateByPrimaryKey(employee);
        }
        List<Role> roles = employee.getRoles();
        if(roles != null){
            for (Role role : roles) {
                employeeMapper.insertRoleRelation(employee.getId(),role.getId());
            }
        }
    }

    public void changePassword(Long id, String newPassword) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        newPassword = new Md5Hash(newPassword,employee.getUsername(),2).toString();
        employeeMapper.updatePasswordById(id,newPassword);
    }

    public Employee login(String username) {
        return employeeMapper.selectByUsername(username);
    }

}
